#include "BranchParamDialog.h"
#include <QtGui/QApplication>
#include "Branch.h"


//-------------------------------------

BranchParamDialog::BranchParamDialog( Branch * b, QWidget *parent ) :
    QDialog(parent),
    branch( 0 )
{
    branch = b;

    resize( 215, 290 );

    buttonBox = new QDialogButtonBox( this );
    buttonBox->setGeometry( QRect( 30, 250, 160, 30 ) );
    buttonBox->setOrientation( Qt::Horizontal );
    buttonBox->setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
    groupBox = new QGroupBox( this );
    groupBox->setGeometry( QRect( 5, 0, 205, 245 ) );
    wgtLineEdits = new QWidget( groupBox );
    wgtLineEdits->setGeometry( QRect( 60, 20, 110, 200 ) );
    vlayLineEdits = new QVBoxLayout( wgtLineEdits );
    vlayLineEdits->setContentsMargins( 0, 0, 0, 0 );
    ledVbegin = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledVbegin );
    ledVend = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledVend );
    cbState = new QCheckBox( wgtLineEdits );
    vlayLineEdits->addWidget( cbState );
    ledR = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledR );
    ledX = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledX );
    ledYc = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledYc );
    ledKt = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledKt );
    ledImax = new QLineEdit( wgtLineEdits );
    vlayLineEdits->addWidget( ledImax );
    chbAutoColor = new QCheckBox( wgtLineEdits );



    wgtLabels = new QWidget( groupBox );
    wgtLabels->setGeometry( QRect( 10, 20, 40, 200 ) );

    vlayLabels = new QVBoxLayout( wgtLabels );
    vlayLabels->setContentsMargins( 0, 0, 0, 0 );
    lbl1 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl1 );
    lbl2 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl2 );
    lbl3 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl3 );
    lbl4 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl4 );
    lbl5 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl5 );
    lbl6 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl6 );
    lbl7 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl7 );
    lbl8 = new QLabel( wgtLabels );
    vlayLabels->addWidget( lbl8 );
    lbl9 = new QLabel( wgtLabels );


    vlayLineEdits->addWidget( lbl9 );
    vlayLabels->addWidget( chbAutoColor );

    QObject::connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ));
    QObject::connect( buttonBox, SIGNAL( rejected() ), this, SLOT( reject() ));

    QMetaObject::connectSlotsByName(this);

    setWindowTitle(QApplication::translate("BranchParamDialog", "Branch Parameters Dialog", 0, QApplication::UnicodeUTF8));
    groupBox->setTitle(QApplication::translate("BranchParamDialog", "Branch Parameters", 0, QApplication::UnicodeUTF8));
    lbl1->setText(QApplication::translate("BranchParamDialog", "Nbegin:", 0, QApplication::UnicodeUTF8));
    lbl2->setText(QApplication::translate("BranchParamDialog", "Nend:", 0, QApplication::UnicodeUTF8));
    lbl3->setText(QApplication::translate("BranchParamDialog", "State:", 0, QApplication::UnicodeUTF8));
    lbl4->setText(QApplication::translate("BranchParamDialog", "R:", 0, QApplication::UnicodeUTF8));
    lbl5->setText(QApplication::translate("BranchParamDialog", "X:", 0, QApplication::UnicodeUTF8));
    lbl6->setText(QApplication::translate("BranchParamDialog", "Yc:", 0, QApplication::UnicodeUTF8));
    lbl7->setText(QApplication::translate("BranchParamDialog", "Kt:", 0, QApplication::UnicodeUTF8));
    lbl8->setText(QApplication::translate("BranchParamDialog", "Imax:", 0, QApplication::UnicodeUTF8));
    lbl9->setText(QApplication::translate("BranchParamDialog", "Atuo Color", 0, QApplication::UnicodeUTF8));

    fillControls();
}

//-------------------------------------

int BranchParamDialog::Exec()
{
    int res = exec();
    if( res == QDialog::Accepted )
    {
        branch->Nbegin = ledVbegin->text().toInt();
        branch->Nend = ledVend->text().toInt();

        branch->state = cbState->isChecked();

        branch->R = ledR->text().toFloat();
        branch->X = ledX->text().toFloat();
        branch->Yc = ledYc->text().toFloat();
        branch->Kt = ledKt->text().toFloat();
        branch->Imax = ledImax->text().toFloat();

        branch->SetAutoColor( chbAutoColor->isChecked() );
    }
    return res;
}

//-------------------------------------

void BranchParamDialog::fillControls()
{
    if( branch )
    {
        ledVbegin->setText( QString::number( branch->Nbegin ) );
        ledVend->setText( QString::number( branch->Nend ) );

        cbState->setChecked( branch->state );

        ledR->setText( QString::number( branch->R ) );
        ledX->setText( QString::number( branch->X ) );
        ledYc->setText( QString::number( branch->Yc ) );
        ledKt->setText( QString::number( branch->Kt ) );
        ledImax->setText( QString::number( branch->Imax ) );

        chbAutoColor->setChecked( branch->GetAutoColor() );
    }
}

//-------------------------------------

